package yt.kpool.activity.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.R;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.MinerModelDetailsBean;
import yt.kpool.fragment.HomeSecondAFg;
import yt.kpool.fragment.HomeSecondBFg;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 首页列表点击的跳转页面
 */


public class HomeSecondAc extends AppCompatActivity {
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    private LoadingDialog dialog;
    private MinerModelDetailsBean minerModelDetailsBean;
    private List<MinerModelDetailsBean.DataBean.CoinListBean> listData=new ArrayList<>();


    public static void start(Context mactivity, String ID,String Name) {
        Intent intent = new Intent(mactivity, HomeSecondAc.class);
        intent.putExtra("ID", ID);
        intent.putExtra("Name", Name);
        mactivity.startActivity(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homesecond);
        ButterKnife.bind(this);
        initTitle();
        init();
        if(NetWorkUtil.checkNet(HomeSecondAc.this))
        {
            getNetData(getIntent().getStringExtra("ID"));
        }
        else
        {

            ToastUtil.showToast(HomeSecondAc.this,getResources().getString(R.string.internet_error));
        }
    }

    /**
     * 设置标题栏
     */
    private void initTitle() {
        TitleControl t = new TitleControl(this);
        t.setTitle(getIntent().getStringExtra("Name"));

    }

    private void init()
    {
        dialog = new LoadingDialog(HomeSecondAc.this, 0);

    }

    public class MyAdapter extends FragmentPagerAdapter {
        List<Fragment> fragments;
        List<String> list;
     public MyAdapter(FragmentManager fm, List<Fragment> fragments, List<String> list) {
        super(fm);
        this.fragments=fragments;
        this.list=list;
        }


        @Override
        public Fragment getItem(int position) {
        return fragments.get(position);
         }


         @Override
         public int getCount() {
         return fragments.size();
         }


        @Override
        public CharSequence getPageTitle(int position) {
          return list.get(position);
         }
            }


    /**
     * 获取首页网络数据
     */
    private void getNetData(String CurrencyCode)
    {

        dialog.show();


        NetHelperNew.MinerModelDetails(CurrencyCode, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                minerModelDetailsBean = MyGson.getInstance().fromJson(data, MinerModelDetailsBean.class);

//                for(int i=0;i<2;i++)
//                {
//                    listData.add(minerModelDetailsBean.getData().getCoinList().get(i));
//                }
//                HomeListBean.DataBean.MinerModelListBean bean=new HomeListBean.DataBean.MinerModelListBean();
//                listData.add(bean);
//                listData.add(bean);
//                listData.add(bean);
//                listData.add(bean);
//                listData.add(bean);
//                listData.add(bean);
//                listData.add(bean);
//                listData.add(bean);

                BaseApplication.Exchange=minerModelDetailsBean.getData().getExchange();
                List<Fragment> list=new ArrayList<>();
                list.add(new HomeSecondAFg(minerModelDetailsBean));
                list.add(new HomeSecondBFg(minerModelDetailsBean,getIntent().getStringExtra("Name")));
                List<String> listt=new ArrayList<>();
                listt.add("挖矿收益");
                listt.add("矿机信息");
                tablayout.setupWithViewPager(viewpager);
                viewpager.setAdapter(new MyAdapter(getSupportFragmentManager(), list,listt));

                dialog.dismiss();


            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                dialog.dismiss();
                ToastUtil.showToast(HomeSecondAc.this,msg);
            }
        });


    }

}
